" use for write c
augroup cprog
  " Set some sensible defaults for editing C-files
  " Remove all cprog autocommands
  au!

  " When starting to edit a file:
  "   For *.c and *.h files set formatting of comments and set C-indenting on.
  "   For other files switch it off.
  "   Don't change the order, it's important that the line with * comes first.
  "autocmd BufRead *.cpp,*.c,*.h 1;/^{
  autocmd BufNewFile *.c,*.C,*.cc,*.cpp,*.cxx,*.h,*.hpp  call Lcpp()
  autocmd BufNewFile *.sh  call Lsh()
  autocmd BufEnter *.cpp,*.c,*.h abbr _dtrace
	\ #define dtrace	do { fprintf(stdout, "\033[36mTRACE"      \<CR>
	\				"\033[1;34m==>\033[33m%16s"       \<CR>
	\				"\033[36m: \033[32m%4d\033[36m: " \<CR>
	\				"\033[35m%-24s \033[34m"          \<CR>
	\				"[\033[0;37m%s\033[1;34m,"        \<CR>
	\				" \033[0;36m%s\033[1;34m]"        \<CR>
	\				"\033[0m\n", __FILE__, __LINE__,  \<CR>
	\				__FUNCTION__ /* __func__ */,      \<CR>
	\				__TIME__, __DATE__);              \<CR>
	\			} while (0)          /* defined by mhfan */
""  autocmd BufLeave *.cpp,*.c,*.h unabbr _dtrace

  command Ma : call Main_Add()
  func Main_Add() " add main info
  	let l=line(".")
	call append(l+0, "#include <stdio.h>")
	call append(l+1, "#include <stdlib.h>")
	call append(l+2, "#include <string.h>")
	call append(l+3, "int main(int argc, char *argv[])")
	call append(l+4, "{")
	call append(l+5, "")
	call append(l+6, "")
	call append(l+7, "")
	call append(l+8, "    return 0;")
	call append(l+9, "}")
  	exec l+6 .   ""
  endf

  command Dc : call DCOMM()
  func DCOMM()  " delete the block comment macro lines.
  	exec "normal 1l"
  	let l1 = searchpair('^\s*#\s*if\s\+\d\+', '', '^\s*#\s*endif', 'Wb')
  	if l1 < 1
  		return
  	endif
  	exec "normal ]#"
  	if getline(".") =~ '^\s*#\s*else'
  		return
  	endif
  	exec "normal dd" . l1 . "Gdd"
  endf
  
  command Rc :call RCOMM()
  func RCOMM()  " reverse the block comment.
  	exec "normal 1l"
  	if searchpair('^\s*#\s*if\s\+\d\+', '', '^\s*#\s*enif', 'Wb') < 1
  		return
  	endif
  	exec '.s#\d\+#\=submatch(0)==0 ? 1 : 0#'
  endf
  
  command -range Co : call  COMM(<line1>,<line2>)
  func COMM(l1, l2) " add the MACRO comment around the block of C/Cpp code.
"  	  exec a:l2+1 . \"s%^%#endif    /* comment by yuwei.zhang */\<CR>%\"
  	  exec a:l2+1 . "s%^%#endif \<CR>%"
  	  exec a:l1 .   "s%^%#if 0     /* by yuwei.zhang BESOVIDEO on ".strftime("%Y-%m-%d")." */\<CR>%"
  endf

  command CC :call CCOMM()
  func CCOMM()	" convert #define <<-->> #undef
  	let iLn=line(".")
  	let strLn=getline(".")
	if strLn =~ '^\s*#\s*define'
		let strNew=substitute(strLn, "define", "undef", "")
		exec setline(iLn, strNew)
		exec iLn
	elseif strLn =~ '^\s*#\s*undef'
		let strNew=substitute(strLn, "undef", "define", "")
		exec setline(iLn, strNew)
		exec iLn
	endif
  endf

  command PT :call PTRACE()
  func PTRACE()	" ptrace func for cursor

  endf

  func Lcpp()
  	call Title("c")		" diff commect char
  endfun

  func Lsh()
  	call Title("sh")	" comment char
  endfun

  func Title(type)
    let ctype=a:type
  	if strridx(ctype, "sh") == 0
	  let fch="#! \\/bin\\/bash\\r#"
	  let cch="#"
	else
	  let fch="\\/"
	  let cch="*"
	endif

    let fn = strpart(@%, strridx(@%, "/") + 1)
    if strridx(fn,"\.h") > 0
	  let defn = substitute(toupper(fn), "\\.H", "_DEF_H", "")
	  let cplus="#ifdef __cplusplus\rextern \"C\" {\r#endif\r"
	  let defh = "#ifndef  __" . defn . "\r#define  __" . defn . "\r\r" . cplus
    else
	  let defh = "\r\r"
    endif

    if strridx(fn,"\.h") > 0
      let cplus="#ifdef __cplusplus\r}\r#endif\r\r"
      let defh="\r\r\r" . cplus . "#endif\\/\\/ __" . defn . "\r"
    endif

 	if strridx(ctype, "sh") == 0
	  let fch="#"
	else
	  let fch="\\/"
	endif

""    let rspace = "****************"
""	execute "$g/$/s//" . defh . fch .
""		\ strpart(rspace, 3, 24-strlen(fn)/2) . " End Of File: " . fn . " " .
""		\ strpart(rspace, 0, 24-strlen(fn)/2) . '\/'
""
""	execute 19
  endfun
augroup END

